#version 120

attribute vec3  VertexPosition;
attribute float VertexStartTime;
attribute vec3  VertexVelocity;
attribute vec3  VertexInitialVelocity;

varying vec3  Position;     // To transform feedback
varying float StartTime;    // To transform feedback
varying vec3  Velocity;     // To transform feedback

uniform float Time;     // Animation time
uniform float H;        // Elapsed time between frames
uniform vec3  Accel;     // Particle acceleration
uniform float ParticleLifetime; // Max particle lifetime

void main()
{
  Position  = VertexPosition;
  Velocity  = VertexVelocity;
  StartTime = VertexStartTime;
  
  if (Time >= StartTime) {
    float age = Time - StartTime;
    
    if (age > ParticleLifetime) {
      // The particle is past its lifetime, recycle
      Position.y = 0.0;
      Velocity = VertexInitialVelocity;
      StartTime = Time;
    } else {
      // The particle is alive, update
      Position += Velocity * H;
      Velocity += Accel * H;
    }
  }
  
  gl_Position = vec4(Position, 1.0);
}
